/**
 * 
 */
package com.jolestar.yayaweibotong.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.Validate;

import com.jolestar.yayaweibotong.YaYaConstants;
import com.jolestar.yayaweibotong.dao.UserDomain;
import com.jolestar.yayaweibotong.service.UserService;
import com.jolestar.yayaweibotong.util.YaYaUtils;

/**
 * @author jolestar@gmail.com
 * 
 */
public class RegisterAction extends AbstractAction {

	/**
	 * 
	 */
	public RegisterAction() {
		super("register");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.jolestar.yayaweibotong.controller.AbstractAction#doAction(javax.servlet
	 * .http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected String doAction(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setAttribute("invite_enable", YaYaConstants.INVITE_ENABLE);
		
		if (request.getMethod().equalsIgnoreCase("get")) {
			String inviteToken = request.getParameter("invite_token");
			request.setAttribute("invite_token", inviteToken == null ? ""
					: inviteToken);
			return "register";
		} else {
			String email = request.getParameter("email");
			String password = request.getParameter("password");
			String repassword = request.getParameter("repassword");
			String inviteToken = request.getParameter("invite_token");
			request.setAttribute("email", email == null ? "" : email);
			request.setAttribute("password", password == null ? "" : password);
			request.setAttribute("repassword", repassword == null ? ""
					: repassword);
			request.setAttribute("invite_token", inviteToken == null ? ""
					: inviteToken);

			if (StringUtils.isBlank(email) || StringUtils.isBlank(password)
					|| StringUtils.isBlank(repassword)) {
				return this.sendError(request, "请填写完整.");
			}
			if (!YaYaUtils.isValidEmail(email)) {
				return this.sendError(request, "邮件格式不正确.");
			}
			UserDomain user = UserService.ServiceHolder.getInstance().getUser(
					email);
			if (user != null) {
				return this.sendError(request,
						"此邮件已经被注册，请使用别的邮箱或者 点击<a href=\"login\">这里</a>登录.");
			}
			if (YaYaConstants.INVITE_ENABLE) {
				if (StringUtils.isBlank(inviteToken)) {
					return this.sendError(request, "需要邀请码.");
				}
				if (!UserService.ServiceHolder.getInstance()
						.validateInviteToken(inviteToken)) {
					return this.sendError(request, "邀请码无效.");
				}
			}
			if (!StringUtils.equals(password, repassword)) {
				request.setAttribute("repassword", "");
				return this.sendError(request, "两次密码不一致.");
			}
			user = UserService.ServiceHolder.getInstance().createUser(email,
					password, inviteToken);
			request.setAttribute("message", "注册成功。<br/>您的注册密码将会发送到邮箱:" + email);
			request.setAttribute("user", user);
			request.setAttribute("backurl", "/login");
			return "success";
		}
	}

	private String sendError(HttpServletRequest request, String message) {
		request.setAttribute("has_error", true);
		request.setAttribute("error", message);
		return "register";
	}

	@Override
	public String getPath() {
		return "/"+this.getName();
	}

}
